﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;

namespace Tank_Control
{
    public partial class Form1 : Form
    {
        SerialPort ComPort;

        public Form1()
        {
            ComPort = new SerialPort();

            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                ComPort.BaudRate = 9600;
                ComPort.DataBits = 8;
                ComPort.Parity = Parity.None;
                ComPort.StopBits = StopBits.One;
                ComPort.Handshake = Handshake.None;
                ComPort.ReadTimeout = 10;
                ComPort.WriteTimeout = 10;
                ComPort.PortName = textBox1.Text;
                ComPort.Open();

                ComPort.WriteLine("i");
                string board_id = ComPort.ReadLine();
                board_id = ComPort.ReadLine();
                if (board_id != "<A>STM32VLD")
                    throw (new Exception("Wrong board id"));
                ComPort.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived);
                label2.Text = "Connected!";

                button2.Enabled = true;
                button3.Enabled = true;
                button4.Enabled = true;
                button5.Enabled = true;
                button8.Enabled = true;
                button6.Enabled = true;
                button7.Enabled = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show("Device is not found\nReason: " + ex.Message);
                this.Close();
            }

        }

        private void button2_Click(object sender, EventArgs e)
        {
            ComPort.WriteLine("f");
        }

        private void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            this.Invoke(new EventHandler(DoUpdate));
        }

        private void DoUpdate(object s, EventArgs e)
        {
            textBox2.Text += ComPort.ReadExisting();
        }

        private void button6_Click(object sender, EventArgs e)
        {
            string channel = textBox3.Text;
            Int16 speed = Convert.ToInt16(textBox4.Text);
            string cmd = "s" + channel + speed.ToString("X4");         

            ComPort.WriteLine(cmd);
        }

        private void button7_Click(object sender, EventArgs e)
        {
            string channel = textBox3.Text;
            string cmd = "g" + channel;

            ComPort.WriteLine(cmd);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            ComPort.WriteLine("b");
        }

        private void button5_Click(object sender, EventArgs e)
        {
            ComPort.WriteLine("r");
        }

        private void button4_Click(object sender, EventArgs e)
        {
            ComPort.WriteLine("l");
        }

        private void button8_Click(object sender, EventArgs e)
        {
            ComPort.WriteLine("S");
        }
    }
}
